<?php
class Amounts_3d
{

    public $table_name;
    public $db;
    public $tbl;
    public $gametype;

    public function Amounts_3d( $gametype = 1 )
    {
        global $g_gametype_3d;
        $this->table_name = "amounts_".$g_gametype_3d[$gametype]['en'];
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->db = new DB();
        $this->gametype = $gametype;
    }

    public function getBackArr( $bettype, $id, $disk, $subid = 0 )
    {
        $reVal = false;
        $sql = "SELECT back".$disk." FROM {$this->table_name} WHERE user_id =".$id." AND bettype=".$bettype." AND subid=".$subid." LIMIT 1";
        $result = $this->db->query( $sql );
        if ( $result )
        {
            $row = mysql_fetch_array( $result );
            $reVal = floatval( $row[0] );
        }
        return $reVal;
    }

    public function getAmountsDataById( $bettype, $user_id, $subid = 0 )
    {
        $reVal = false;
        $sql = "SELECT * FROM ".$this->table_name;
        $sql .= " WHERE";
        $sql .= " bettype = ".$bettype;
        $sql .= " AND subid = ".$subid;
        $sql .= " AND user_id = ".$user_id;
        $sql .= " LIMIT 1";
        $result = $this->db->query( $sql );
        if ( $result )
        {
            $row = mysql_fetch_array( $result );
            $reVal = $row;
        }
        else
        {
            $reVal = false;
        }
        return $reVal;
    }

    public function getAmountsRsById( $user_id )
    {
        global $g_onlyN5Arr;
        $type35 = getShow35( $this->gametype );
        if ( $this->gametype == 20 )
        {
            $type35 = true;
        }
        $strOnlyN5Arr = implode( ",", $g_onlyN5Arr );
        $rs = new RecordSet();
		if($this->gametype==25 || $this->gametype==26)
		{
			$sql = "SELECT * FROM ".$this->table_name;
            $sql .= " WHERE";
            $sql .= " user_id = ".$user_id." and subid = '0'";
            $sql .= " ORDER BY bettype ";
            $rs->open( $sql, 0, 20 );	
		}
        else if ( $type35 == false )
        {
            $sql = "SELECT * FROM ".$this->table_name;
            $sql .= " WHERE";
            $sql .= " user_id = ".$user_id." and subid = '0' and  bettype not in ({$strOnlyN5Arr})";
            $sql .= " ORDER BY bettype ";
            $rs->open( $sql, 0, 20 );
        }
        else
        {
            $sql = "SELECT * FROM ".$this->table_name;
            $sql .= " WHERE";
            $sql .= " user_id = ".$user_id;
            $sql .= " ORDER BY bettype , subid desc";
            $rs->open( $sql, 0, 20 );
        }
        return $rs;
    }

    public function check_Amount_255( $lotto_id, $bettype, $numbers, $val, $subid = 0 )
    {
        $tblName = "autorate_".$this->tbl;
        $cfgName = "autorate_config_".$this->tbl;
        $reVal = array();
        $sql = "";
        if ( $bettype == 46 )
        {
            $NumsArrayStr = implode( ",", $numbers );
            $sql = "!!SELECT amount_255,"." (Select max(total) from {$tblName} where lotto_id={$lotto_id} AND numbers = '{$NumsArrayStr}' AND bettype = A.bettype_id AND subid = A.subid) as total"." FROM {$cfgName} A WHERE bettype_id = ".$bettype." AND subid = {$subid}";
        }
        else if ( is_array( $numbers ) )
        {
            $NumsArray = implode( ",", $numbers );
            $sql = "!!SELECT amount_255,"." (Select max(total) from {$tblName} where lotto_id={$lotto_id} AND numbers in ({$NumsArray}) AND bettype = A.bettype_id AND subid = A.subid) as total"." FROM {$cfgName} A WHERE bettype_id = ".$bettype." AND subid = {$subid}";
        }
        else if ( $bettype == 20 && $this->check_3CharSpecNumAmt255( $this->tbl, $numbers ) )
        {
            $sql = "!!SELECT amount_255,"." (Select max(total) from {$tblName} where lotto_id={$lotto_id} AND numbers = '{$numbers}' AND bettype = 20 AND subid = A.subid) as total"." FROM {$cfgName} A WHERE bettype_id = 200 AND subid = {$subid}";
        }
        else
        {
            $sql = "!!SELECT amount_255,"." (Select max(total) from {$tblName} where lotto_id={$lotto_id} AND numbers = '{$numbers}' AND bettype = A.bettype_id AND subid = A.subid) as total"." FROM {$cfgName} A WHERE bettype_id = ".$bettype." AND subid = {$subid}";
        }
        $result = $this->db->query( $sql );
        $amount_255 = 0;
        $total = 0;
        if ( $result )
        {
            $row = mysql_fetch_array( $result );
            $amount_255 = intval( $row[0] );
            $total = intval( $row[1] ) + 0;
            if ( $total < 0 )
            {
                $total = 0;
            }
        }
        if ( 0 < $amount_255 && $amount_255 < $total + $val )
        {
            if ( is_array( $numbers ) )
            {
                $nval = $amount_255 - $val;
                $sql = "Select numbers from {$tblName} where lotto_id={$lotto_id} AND numbers in ({$NumsArray}) AND bettype = {$bettype} AND subid = {$subid} and total>{$nval}";
                $result = $this->db->query( $sql );
                while ( $row = mysql_fetch_array( $result ) )
                {
                    array_push( $reVal, $row[0] );
                }
            }
            array_push( $reVal, $numbers );
        }
        return $reVal;
    }

    public function check_3CharSpecNumAmt255( $tbl, $number, $subid = 0 )
    {
        $SpecNumbersId = "3CharSpecNumbers_{$tbl}_{$subid}";
        $numArr = array();
        $sql = "!!Select K.value,C.amount_255 From k_config K Left Join autorate_config_{$tbl} C ON (C.bettype_id=200 AND C.subid = {$subid}) Where K.id = '{$SpecNumbersId}'";
        $result = $this->db->query( $sql );
        if ( $result )
        {
            $row = mysql_fetch_array( $result );
            $numArr = explode( ",", strval( $row[0] ) );
            $Amount_255 = intval( $row[1] );
            if ( 0 < $Amount_255 && in_array( $number, $numArr ) )
            {
                return true;
            }
        }
        return false;
    }

}

?>